# -*- coding:utf-8 -*-
"""
@author: fsksf

@since: 2022/4/12 16:57

通用API
"""
import json
from typing import Optional, Awaitable

from pyrestful.rest import get, post, put, delete
from pyrestful import mediatypes
from vcat.server.base_handler import BaseResource

from vcat.server.routes import ALL_ROUTES
from vcat.utils.api_util import add_handler
from vcat.server.status_code import make_response, Code
from vcat.server.server import app
from vcat.server import TABLE_METAS, BROKER_METAS


@add_handler
class NormalAPIResource(BaseResource):

    def data_received(self, chunk: bytes) -> Optional[Awaitable[None]]:
        pass

    @get(_path='/api/normal/page/meta/list', _types=[dict],
         _catch_fire=True, _produces=mediatypes.APPLICATION_JSON,
         _consumes=mediatypes.APPLICATION_JSON)
    def page_meta_list(self, name):
        page_name = name.decode()
        meta = list(TABLE_METAS[page_name].values())
        meta.sort(key=lambda x: x['index'])
        if meta:
            return make_response(code=Code.OK, data=meta)
        else:
            return make_response(code=Code.data_not_exist)

    @get(_path='/api/normal/model/meta', _types=[dict],
         _catch_fire=True, _produces=mediatypes.APPLICATION_JSON,
         _consumes=mediatypes.APPLICATION_JSON)
    def meta_info(self, model):
        meta_name = model.decode()
        meta = TABLE_METAS[meta_name].values()
        if meta:
            return make_response(code=Code.OK, data=meta)
        else:
            return make_response(code=Code.data_not_exist)

    @get(_path='/api/normal/model/data', _types=[dict],
         _catch_fire=True, _produces=mediatypes.APPLICATION_JSON,
         _consumes=mediatypes.APPLICATION_JSON)
    def model_data(self, event_name):
        event_name = event_name.decode()
        data = app.engine.get_event_datas(event_name)
        return make_response(code=Code.OK, data=[x.to_dict() for x in data])


    @get(_path='/api/normal/router', _types=[dict],
         _catch_fire=True, _produces=mediatypes.APPLICATION_JSON,
         _consumes=mediatypes.APPLICATION_JSON)
    def routers(self):
        pages = [p.to_dict() for p in ALL_ROUTES]

        return make_response(code=Code.OK, data=pages)

    @get(_path='/api/normal/strategy/classes', _types=[dict],
         _catch_fire=True, _produces=mediatypes.APPLICATION_JSON,
         _consumes=mediatypes.APPLICATION_JSON)
    def strategy_classes(self):
        strategies = app.engine.get_all_strategy_class()
        classes_info = [stra.meta() for stra in strategies]

        return make_response(code=Code.OK, data=classes_info)

    @get(_path='/api/normal/strategy/clsParams', _types=[dict],
         _catch_fire=True, _produces=mediatypes.APPLICATION_JSON,
         _consumes=mediatypes.APPLICATION_JSON)
    def strategy_cls_params(self, name):
        name = name.decode()
        strategy_cls = app.engine.get_strategy_cls_by_name(name)
        data = strategy_cls.get_init_params()

        return make_response(code=Code.OK, data=data)
